<template>
  <div class="carousel-container" @touchstart="onTouchStart" @touchend="onTouchEnd">
    <div class="carousel">
      <div
          v-for="(item, index) in items"
          :key="index"
          :class="['carousel-item', { active: currentIndex === index }]"
      >
        <img :src="item.image" alt="Slide" class="img"/>
        <div class="text">{{item.info}}</div>
      </div>
    </div>
    <div class="carousel-indicators">
      <span
          v-for="(item, index) in items"
          :key="index"
          :class="['indicator', { active: currentIndex === index }]"
          @click="goToSlide(index)"
      ></span>
    </div>
  </div>

</template>

<script>
export default {
  name: 'Carousel',
  props: {
    items: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      currentIndex: 0,
      touchStartX: 0,
      autoPlayInterval: 3,
    };
  },
  methods: {
    goToSlide(index) {
      this.currentIndex = index;
      this.stopAutoPlay();
      this.startAutoPlay();
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length;
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.items.length;
    },
    onTouchStart(event) {
      this.touchStartX = event.touches[0].clientX;
    },
    onTouchEnd(event) {
      const touchEndX = event.changedTouches[0].clientX;
      const diffX = touchEndX - this.touchStartX;
      if (diffX > 50) {
        this.prevSlide();
      } else if (diffX < -50) {
        this.nextSlide();
      }
    },
    startAutoPlay() {
      this.autoPlayInterval = setInterval(this.nextSlide, 3000);
    },
    stopAutoPlay() {
      clearInterval(this.autoPlayInterval);
    },
  },
  watch: {
    currentIndex() {
      this.stopAutoPlay();
      this.startAutoPlay();
    },
  },
};
</script>

<style scoped lang="scss">
.carousel-container {
  position: relative;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  overflow: hidden;
  height: 300px;
  z-index: 1;
}

.carousel {
  display: flex;
  transition: transform 0.3s ease-in-out;
  .img{
    width: 360px;
    height: 200px;
  }
  .text{
      font-size: 16px;
      font-family: "SourceHanSansCN";
      color: rgb(255, 255, 255);
      margin-top: -35px;
    margin-left: -120px;
  }
}

.carousel-item {
  flex-shrink: 0;
  width: 100%;
  height: 300px;
  overflow: hidden;
  opacity: 1;
  transition: opacity 0.3s ease-in-out;
  z-index: 1;
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-controls {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.carousel-controls button {
  margin: 0 10px;
  padding: 5px 10px;
  background-color: #333;
  color: white;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s;
}

.carousel-controls button:hover {
  background-color: #555;
}


.carousel-indicators {
  display: flex;
  right: 30px;
  margin-top: -120px;
  position: absolute;
}

.indicator {
  width: 8px;
  height: 8px;
  margin: 0 5px;
  border-radius: 50%;
  background-color: #ccc;
  cursor: pointer;
  transition: background-color 0.3s;
}

.indicator.active {
  background-color: #007bff;
}
</style>
